package com.engine.ch.action;

import com.engine.api.gdyjs.service.impl.AssetsServiceImpl;
import com.engine.ch.entity.Archive;
import com.engine.ch.service.impl.SyncArchiveImpl;
import com.engine.workflow.biz.requestForm.WfToDocBiz;
import weaver.conn.RecordSet;
import weaver.formmode.customjavacode.AbstractModeExpandJavaCodeNew;
import weaver.general.Util;
import weaver.hrm.User;
import weaver.interfaces.workflow.action.Action;
import weaver.interfaces.workflow.action.WorkflowToDoc;
import weaver.soa.workflow.request.MainTableInfo;
import weaver.soa.workflow.request.Property;
import weaver.soa.workflow.request.RequestInfo;
import com.engine.workflow.service.HtmlToPdfService;
import com.engine.workflow.service.impl.HtmlToPdfServiceImpl;
import com.engine.common.util.ServiceUtil;
import weaver.workflow.request.RequestManager;


import java.io.File;
import java.util.*;
import java.util.logging.Logger;

public class SyncArchiveAction extends AbstractModeExpandJavaCodeNew {
    private static Logger logger = Logger.getLogger(AssetsServiceImpl.class.getName());

    @Override
    public Map<String, String> doModeExpand(Map<String, Object> map) {
        User user = (User)map.get("user");
        Map<String, String> result = new HashMap<String, String>();
        SyncArchiveImpl syncArchive = new SyncArchiveImpl();
        try {
            int billid = -1;//数据id
            int modeid = -1;//模块id
            RequestInfo requestInfo = (RequestInfo)map.get("RequestInfo");
            if(requestInfo!=null){
                billid = Util.getIntValue(requestInfo.getRequestid());
                modeid = Util.getIntValue(requestInfo.getWorkflowid());
                if(billid>0&&modeid>0){
                    //------请在下面编写业务逻辑代码------//
                    MainTableInfo mainTableInfo = requestInfo.getMainTableInfo();
                    Property[] property = mainTableInfo.getProperty();
                    String lcrequestid = "";
                    String wjid = "";
                    String bt = "";
                    String szbm = "";
                    String wjbhfwzh = "";
                    String zrzgs = "";
                    String zrzbm = "";
                    String cwrq = "";
                    int ys = 0;
                    String mj = "";
                    String ndhfwzh = "";
                    String bgqx = "";
                    String fj = "";
                    String zw = "";
                    String spjlsfwdpdf = "";
                    String fwrq = "";
                    String swsj = "";
                    String gwlx = "";
                    String zsdw = "";
                    Integer tsjg = 0;
                    String tsjgsm = "";
                    String tssj = "";
                    Integer sflssj = 0;
                    String lwdw = "";
                    for (Property property1 : property) {
                        String name = property1.getName();
                        String value = property1.getValue();

                        if(name.equals("lcrequestid")){
                            lcrequestid = value;
                            Map<String, Object> params = new HashMap<>();
                            params.put("useWk","1"); //是否使用wkhtmltopdf插件转pdf 1：是  0：否  不传则默认使用Itext插件
                            params.put("requestid",lcrequestid);  //必传
                            params.put("modeid","123456");  //模板id(传模板id则根据模板生成.不传则默认使用显示模板)
                            params.put("path",""); //存储路径(不传则windows默认D:/testpdf;linux默认/usr/testpdf)
                            params.put("onlyHtml","0"); //0:转pdf  1:只转成html  2:转html和pdf  (不传则默认=0)
                            params.put("keepsign","1");  //1:保留底部签字意见 0：不保留 (不传则默认=1)
                            params.put("pageSize","100"); //底部签字意见最大显示数量  (默认=100)
                            params.put("isTest", "1"); //外部调用必传isTest=1
                            params.put("limitauth", "0"); //不校验权限
                            HtmlToPdfService htmlToPdfService = (HtmlToPdfService) ServiceUtil.getService(HtmlToPdfServiceImpl.class,user);
                            Map<String,Object> pathMap = htmlToPdfService.getFormDatas(params);
                            Object path = pathMap.get("path");
                            Object filename = pathMap.get("filename");
                            WorkflowToPdfAction workflowToDoc = new WorkflowToPdfAction();
                            // 保存ImageFile
                            File file = new File(path.toString());
                            long fileSize = file.length();
                            int i = workflowToDoc.saveImageFile(filename.toString(), path.toString(), fileSize, true);

                            // 保存docdetail
                            WfToDocBiz wfToDocBiz = new WfToDocBiz();
                            RequestManager requestManager =  new RequestManager();
                            workflowToDoc.saveDocDetail(filename.toString(),String.valueOf(modeid),lcrequestid,"",user.getLoginid(),new LinkedHashMap<>(),requestManager,wfToDocBiz);
                            Integer fileId = workflowToDoc.getFileId();
                            // 保存DocImageFile
                            workflowToDoc.saveDocImageFile(fileId,i,filename.toString(),3);
                            spjlsfwdpdf = String.valueOf(fileId);
                        }else if(name.equals("wjid")){
                            wjid = value;
                        }else if(name.equals("bt")){
                            bt = value;
                        }else if(name.equals("szbm")){
                            szbm = value;
                        }else if(name.equals("wjbhfwzh")){
                            wjbhfwzh = value;
                        }else if(name.equals("zrzgs")){
                            zrzgs = value;
                        }else if(name.equals("zrzbm")){
                            zrzbm = value;
                        }else if(name.equals("cwrq")){
                            cwrq = value;
                        }else if(name.equals("ys")){
                            ys = Util.getIntValue(value);
                        }else if(name.equals("mj")){
                            mj = value;
                        }else if(name.equals("ndhfwzh")){
                            ndhfwzh = value;
                        }else if(name.equals("bgqx")){
                            bgqx = value;
                        }else if(name.equals("fj")){
                            fj = value;
                        }else if(name.equals("zw")){
                            zw = value;
                        }else if(name.equals("spjlsfwdpdf")){
//                            spjlsfwdpdf = value;
                        }else if(name.equals("fwrq")){
                            fwrq = value;
                        }else if(name.equals("swsj")){
                            swsj = value;
                        }else if(name.equals("gwlx")){
                            gwlx = value;
                        }else if(name.equals("zsdw")){
                            zsdw = value;
                        }else if(name.equals("tsjg")){
                            tsjg = Util.getIntValue(value);
                        }else if(name.equals("tsjgsm")){
                            tsjgsm = value;
                        }else if(name.equals("tssj")){
                            tssj = value;
                        }else if(name.equals("sflssj")){
                            sflssj = Util.getIntValue(value);
                        }else if(name.equals("lwdw")){
                            lwdw = value;
                        }
                    }
                    Archive archive = new Archive(lcrequestid,wjid,bt,szbm,wjbhfwzh,zrzgs,zrzbm,cwrq,ys,mj,ndhfwzh,bgqx,fj,zw,spjlsfwdpdf,fwrq,swsj,gwlx,zsdw,tsjg,tsjgsm,tssj,sflssj,lwdw);
                    syncArchive.sync(archive);
                }
            }
        } catch (Exception e) {
            result.put("errmsg","自定义出错信息");
            result.put("flag", "false");
        }
        return result;
    }
}
